﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace Thumbnail
{
    public partial class Form1 : Form
    {
        #region members
        private Video _video;
        private VideoProperties _props;
        private string _fileName;
        #endregion

        #region Properties
        private string FileName
        {
            get
            {
                return _fileName;
            }
            set
            {
                _fileName = value;
                FileNameChanged();
            }
        }
        #endregion

        #region ctor
        public Form1(string fileName = "")
        {
            InitializeComponent();
            FileName = fileName;
        }
        #endregion

        #region events
        private void tbSecound_Scroll(object sender, EventArgs e)
        {
            lblCurrent.Text = string.Format("{0} Secound {1}", tbSecound.Value, _props.Duration);
            SetCurrentImage();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            // because the Main method is not define with [STAThread] we cant open dialog,
            // so we create a new thread with ApartmentState.STE and the dialog is open in the new thread.

            AutoResetEvent are = new AutoResetEvent(false);
            DialogResult dialogResult = DialogResult.Yes;

            Thread thread = new Thread(() =>
            {
                dialogResult = openFileDialog1.ShowDialog();
                are.Set();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            are.WaitOne();

            if (dialogResult == DialogResult.OK)
            {
                FileName = openFileDialog1.FileName;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // because the Main method is not define with [STAThread] we cant open dialog,
            // so we create a new thread with ApartmentState.STE and the dialog is open in the new thread.

            AutoResetEvent are = new AutoResetEvent(false);
            DialogResult dialogResult = DialogResult.Yes;
            string currentSecound = tbSecound.Value.ToString();

            Thread thread = new Thread(() =>
            {
                saveFileDialog1.FileName = currentSecound + ".jpg";
                dialogResult = saveFileDialog1.ShowDialog();
                are.Set();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            are.WaitOne();

            if (dialogResult == DialogResult.OK)
            {
                picScreen.Image.Save(saveFileDialog1.FileName);
            }
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://blogs.microsoft.co.il/blogs/shlomo/");
        }
        #endregion

        #region method
        private void FileNameChanged()
        {
            if (!string.IsNullOrEmpty(FileName))
            {
                txtMovie.Text = FileName;
                _props = new VideoProperties(FileName);
                _video = new Video(FileName);
                lblCurrent.Text = string.Format("0 Secound {0}", _props.Duration);

                tbSecound.Maximum = (int)_props.Duration;
                SetCurrentImage();
            }
        }
        
        private void SetCurrentImage()
        {
            var pic = _video.GetBitmap(tbSecound.Value, _props.Width, _props.Height);
            picScreen.Image = pic;
        }
        #endregion
    }
}
